// 在浏览器中调起 breadtrip App, 依赖于wechatjssdk widget
window.BT = window.BT || {};
BT.OpenApp = {
    parseUrl: function (options) {
        var url = "",
            iosUrl = "",
            androidUrl = "";
        /*  
            根据传入类型，返回app url schema,传入参数可选数据结构如下
            {
                pageType: 'trip',
                tripId : 1
            }
            {
                pageType: 'waypoint',
                tripId : 1,
                waypointId:1
            }
            {
                pageType: 'comment',
                tripId : 1,
                waypointId:1
            }
            {
                pageType: 'user',
                userid : 1
            }
            {
                pageType: 'country_city',
                destType : 1,
                destId:1,
                destName: "xxxx"
            }
            {
                pageType: 'poi',
                destType : 1,
                destId:1,
                destName: "xxxx"
            }
            {
                pageType: 'webview',
                url : "http://xxx"
            }
            {
                pageType: 'explore'
            }
            {
                pageType: 'newtrip',
                tripId : 1,
                spotId : 1,
                mode : "[big|small]"
            }
            {
                pageType: 'spot',
                spotId : 1
            }
            {
                pageType: 'addspot',
                type : 1 ,
                targetId : 1
            }
            返回数据结构如下，有的页面可能ios和android有特殊url，返回特点url，如果是统一的url，则返回统一url:
            {
                iosUrl : "",
                androidUrl : "",
                url : "" 
            }
        */
        switch(options.pageType){
            case 'trip':
                url = 'breadtrip://browsetrip?tripid=' + options.tripId;
                break;
            case 'waypoint':
                url = 'breadtrip://browsetrip?tripid='+ options.tripId +'&waypointid=' + options.waypointId;
                break;
            case 'comment':
                url = 'breadtrip://comment?tripid='+ options.tripId +'&waypointid=' + options.waypointId;
                break;
            case 'user':
                url = 'breadtrip://userinfo?userid=' + options.userid;
                break;
            case 'country_city':
            case 'poi':
                //ios 5.0.0以下版本不支持携带name参数，故在此先使用不带name参数的url，等5.0.0上之后改为携带name参数的标准格式
                iosUrl = 'breadtrip://destination?type='+ options.destType +'&id=' + options.destId;
                url = 'breadtrip://destination?name='+ encodeURIComponent(options.destName) +'&type='+ options.destType +'&id=' + options.destId;
                break;
            case 'webview':
                url = 'breadtrip://webview?url='+encodeURIComponent(options.url);
                break;
            case 'explore':
                url = 'breadtrip://explore';
                break;
            case 'newtrip':
                url = 'breadtrip://newtrip?tripid=' + options.tripId + ( options.mode ? ('&mode=' + options.mode) : '') + ( options.spotId ? ('&spotid=' + options.spotId ) : '');
                break;
            case 'spot':
                url = 'breadtrip://spot?spotid=' + options.spotId;
                break;
            case 'addspot':
                //添加生活故事，并且可能带上关联的产品类型和id，目前只支持城市猎人 type=1,targetId=产品id
                if(options.type && options.targetId){
                    url = 'breadtrip://addspot?type=' + options.type + '&targetid=' + options.targetId;
                }else{
                    url = 'breadtrip://addspot';
                }
                break;
            default:
                url = "breadtrip://main";
        }
        return {
            iosUrl : iosUrl,
            androidUrl : androidUrl,
            url : url 
        };
    },
    isInWx : function(){
        return RegExp("MicroMessenger","i").test(navigator.userAgent);
    },
    wx: {
        //在微信里判断是否安装了app
        isAppInstalled : function(callback){
            wx.ready(function () {
                wx.invoke('getInstallState', {
                    "packageName": "com.breadtrip",
                    "packageUrl": "breadtrip://",
                }, function(res) {
                    //这里是回调函数
                    if(typeof callback ===  "function"){
                        if(res.err_msg.indexOf('yes') != -1){
                            callback(true);
                        }else{
                            callback(false);
                        }
                    }
                });
            });
        },
        //在微信里调起app的页面
        launchApp : function(urls,callback){
            wx.ready(function () {
                wx.invoke('launch3rdApp', {
                    "appID": "wxe334a1e34a01d971",
                    "messageExt": encodeURI(urls.iosUrl||urls.url),
                    "extInfo": encodeURI(urls.androidUrl||urls.url)
                }, function(res) {                
                    if(typeof callback === "function"){
                        if(res.err_msg.indexOf('ok') != -1){
                            callback(true);
                        }else{
                            callback(false);
                        }
                    }
                });
            });
        }
    },
    other : {
        launchApp: function(url, callback){
            //app外、移动端页面才执行直接调起
            if ( $('body').data('version') == 'None' && $('body').data('user-agent') != 0 ){
                // 尝试直接跳转 App schema
                this.execute(url, callback);
            }
        },
        execute : function(url, callback) {
            //在非微信环境下调起,callback返回调起成功、失败状态
            if (!url) {
                return;
            }
            var node = document.createElement('iframe');
            node.style.display = 'none';
            var body = document.body;
            var timer;
            var clear = function(evt, isTimeout) {
               (typeof callback==='function') &&  callback(!isTimeout);
                window.removeEventListener('pagehide', hide, true);
                window.removeEventListener('pageshow', hide, true);
                if (!node) {
                    return;
                }

                node.onload = null;
                body.removeChild(node);
                node = null;

            };
            var hide = function(e){
                clearTimeout(timer);
                clear(e, false);
            };
            window.addEventListener('pagehide', hide, true);
            window.addEventListener('pageshow', hide, true);
            node.onload = clear;
            node.src = url;
            body.appendChild(node);
            var now = +new Date();
            //如果事件失败，则1秒设置为空
            timer = setTimeout(function(){
                timer = setTimeout(function(){
                  var newTime = +new Date();
                  if(now-newTime>1300){
                    clear(null, false);
                  }else{
                    clear(null, true);
                  }

                }, 1200);
            }, 60);
        }
    }
};